package mt.dummy;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * Thread class for client communication class.
 * A DealServer contains a Socket, an ObjectOutputStream, an ObjectInputStream,
 * a ClientCommDummy and a nickname
 * 
 * @author Grupo45
 *
 */
public class DealWithServer extends Thread {

	private Socket socket;
	private ClientCommDummy clientcomm;
	private ObjectOutputStream out;
	private ObjectInputStream in;
	private String nickname;


	/**
	 * Constructor
	 * @param socket socket of connection established 
	 * @param clientcomm communication client class
	 * @param nickname nickname of client
	 */
	public DealWithServer (Socket socket, ClientCommDummy clientcomm, String nickname) {
		this.socket=socket;
		this.clientcomm=clientcomm;
		this.nickname=nickname;
		try {
			out = new ObjectOutputStream(socket.getOutputStream());
			in = new ObjectInputStream(socket.getInputStream());
			out.writeObject(nickname);
		} catch (IOException e) {
			
		}
	}

	
	/**
	 * Receives message from server and sends them to ClientComm
	 * 
	 * @throws ClassNotFoundException thrown if the class cannot be found
	 * @throws IO Exception
	 */
	public void run(){
		while(true){
			ClientMessage message = null;
			try {
				message = (ClientMessage) in.readObject();
				clientcomm.addMessage(message);
			} catch (ClassNotFoundException e) {
				
			} catch (IOException e) {
				
			}
		}
	}

	/**
	 * 
	 * @return nickname
	 */
	public String getNickname() {
		return nickname;
	}

}
